const baseUrl = "https://prsr.4006889521.cn/StarDream/StarDreamCircleApi/StarDreamCircleInterface/"

const APPID = "wx771addfa45041f6b";
// const SECRET = "2e5cb9accb5d7eb1012455b1594f0d5b";

import FD from './formData'

// 微信的接口，
// 临时使用，实际应该使用后端的
export const getWeixinOpenid = function(JSCODE) {
	return new Promise((resolve, reject) => {
		// uni.request({
		// 	url: `https://api.weixin.qq.com/sns/jscode2session?appid=${APPID}&secret=${SECRET}&js_code=${JSCODE}&grant_type=authorization_code`,
		// 	method:"GET",
		// 	success: (res) => {
		// 		resolve(res.data);
		// 	},
		// 	fail: (res) => {
		// 		reject(res);
		// 	}
		// })
		resolve({ openid: "123456", session_key: "123456" })
	})
}
export const getWeixinToken = function() {
	return new Promise((resolve, reject) => {
		// uni.request({
		// 	url: `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${APPID}&secret=${SECRET}`,
		// 	method:"GET",
		// 	success: (res) => {
		// 		resolve(res.data);
		// 	},
		// 	fail: (res) => {
		// 		reject(res);
		// 	}
		// })
		resolve({ access_token: "123456", expires_in: 7200 })
	})
}

export const getUserPhone = async function(code, access_token) {
	// return await MyRequest(
	// 	`https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=${access_token}`,
	// 	"POST",
	// 	{ code },
	// )
	return {
		phone_info: {
			phoneNumber: "15989193426"
		}
	}
}

// Common Request
async function MyRequest(url, method, data, header) {
	return new Promise((resolve, reject) => {
		uni.request({
			url,
			method,
			data,
			header,
			success: (res) => {
				console.log("Api", `MyRequest called url(${method}) -> ${url.replaceAll(baseUrl, "")} req.data -> ${JSON.stringify(data)}`, res.data);
				resolve(res.data);
			},
			fail: (res) => {
				reject(res);
			}
		})
	})
}

// Common Request multipart - formdata
async function MyRequestForm(url, form) {
	const method = "POST"
	const formData = new FD.FormData();
	Object.keys(form).forEach((key) => {
		let item = form[key]
		if (item.path) { // file
			formData.appendFile(key, item.path, '123.png');
		} else {
			formData.append(key, item);
		}
	})
	const data = formData.getData()
	console.log('data', data)
	return new Promise((resolve, reject) => {
		uni.request({
			url,
			method,
			data: data.buffer,
			header: {
				'Content-Type': data.contentType,
			},
			success: (res) => {
				console.log("Api", `MyRequestForm called url(${method}) -> ${url.replaceAll(baseUrl, "")} req.data -> ${JSON.stringify(form)}`, res.data);
				resolve(res.data);
			},
			fail: (res) => {
				reject(res);
			}
		})
	})
}

// 验证会员注册函数
export const MemgerWhetherRegister = function(MiniOpenId) {
	return MyRequest(
		`${baseUrl}MemgerWhetherRegister?MiniOpenId=${MiniOpenId}`,
		"GET",
	)
}
// 会员注册(修改)提交函数
export const MemgerRegisterWrite = function(
	MiniOpenId,
	MemberName,
	Sex,
	Mobile,
	ProviceName,
	CityName,
	CountyName,
	HabitancyAddress,
) {
	return MyRequest(
		`${baseUrl}MemgerRegisterWrite`,
		"POST",
		{
			MiniOpenId,
			MemberName,
			Sex,
			Mobile,
			ProviceName,
			CityName,
			CountyName,
			HabitancyAddress,
		}
	)
}
// 会员注册(修改)提交函数 form 形式
export const MemgerRegisterWriteForm = function(
	form
) {
	return MyRequest(
		`${baseUrl}MemgerRegisterWrite`,
		"POST",
		form
	)
}

export const ReadBackgroundImageInfor = function() {
	// return new Promise((resolve, reject) => {
	// 	uni.request({
	// 		url: `${baseUrl}ReadBackgroundImageInfor?ImageMark=全部`,
	// 		method:"GET",
	// 		success: (res) => {
	// 			resolve(res.data);
	// 		},
	// 		fail: (res) => {
	// 			reject(res);
	// 		}
	// 	})
	// })
	return MyRequest(
		`${baseUrl}ReadBackgroundImageInfor?ImageMark=全部`,
		"GET",
	)
}

export const ReadBannerImageInfor = function() {
	// return new Promise((resolve, reject) => {
	// 	uni.request({
	// 		url: `${baseUrl}ReadBannerImageInfor?SliderMark=全部`,
	// 		method:"GET",
	// 		success: (res) => {
	// 			resolve(res.data);
	// 		},
	// 		fail: (res) => {
	// 			reject(res);
	// 		}
	// 	})
	// })
	return MyRequest(
		`${baseUrl}ReadBannerImageInfor?SliderMark=全部`,
		"GET",
	)
}

export const ReadFontInfor = function() {
	// return new Promise((resolve, reject) => {
	// 	uni.request({
	// 		url: `${baseUrl}ReadFontInfor`,
	// 		method:"GET",
	// 		success: (res) => {
	// 			resolve(res.data);
	// 		},
	// 		fail: (res) => {
	// 			reject(res);
	// 		}
	// 	})
	// })
	return MyRequest(
		`${baseUrl}ReadFontInfor`,
		"GET",
	)
}

export const ReadChangeLensesRule = function() {
	// return new Promise((resolve, reject) => {
	// 	uni.request({
	// 		url: `${baseUrl}ReadChangeLensesRule`,
	// 		method:"GET",
	// 		success: (res) => {
	// 			resolve(res.data);
	// 		},
	// 		fail: (res) => {
	// 			reject(res);
	// 		}
	// 	})
	// })
	return MyRequest(
		`${baseUrl}ReadChangeLensesRule`,
		"GET",
	)
}

export const ReadNearbyShopInfor = function(ProviceName, CityName, CountyName) {
	// return new Promise((resolve, reject) => {
	// 	uni.request({
	// 		url: `${baseUrl}ReadNearbyShopInfor?ProviceName=${ProviceName}&CityName=${CityName}&CountyName=${CountyName}`,
	// 		method:"GET",
	// 		success: (res) => {
	// 			resolve(res.data);
	// 		},
	// 		fail: (res) => {
	// 			reject(res);
	// 		}
	// 	})
	// })
	return MyRequest(
		`${baseUrl}ReadNearbyShopInfor?ProviceName=${ProviceName}&CityName=${CityName}&CountyName=${CountyName}`,
		"GET",
	)
}

export const ReadIntroduceMainTitle = function() { // 了解星梦圆主标题函数
	// return new Promise((resolve, reject) => {
	// 	uni.request({
	// 		url: `${baseUrl}ReadIntroduceMainTitle`,
	// 		method:"GET",
	// 		success: (res) => {
	// 			resolve(res.data);
	// 		},
	// 		fail: (res) => {
	// 			reject(res);
	// 		}
	// 	})
	// })
	return MyRequest(
		`${baseUrl}ReadIntroduceMainTitle`,
		"GET",
	)
}

export const ReadIntroduceDeputyTitle = function(MainKey) { // 了解星梦圆副标题函数
	// return new Promise((resolve, reject) => {
	// 	uni.request({
	// 		url: `${baseUrl}ReadIntroduceDeputyTitle?MainKey=${MainKey}`,
	// 		method:"GET",
	// 		success: (res) => {
	// 			resolve(res.data);
	// 		},
	// 		fail: (res) => {
	// 			reject(res);
	// 		}
	// 	})
	// })
	return MyRequest(
		`${baseUrl}ReadIntroduceDeputyTitle?MainKey=${MainKey}`,
		"GET",
	)
}

export const ReadIntroduceSubTitle = function(DeputyKey) { // 了解星梦圆子标题函数
	// return new Promise((resolve, reject) => {
	// 	uni.request({
	// 		url: `${baseUrl}ReadIntroduceSubTitle?DeputyKey=${DeputyKey}`,
	// 		method:"GET",
	// 		success: (res) => {
	// 			resolve(res.data);
	// 		},
	// 		fail: (res) => {
	// 			reject(res);
	// 		}
	// 	})
	// })
	return MyRequest(
		`${baseUrl}ReadIntroduceSubTitle?DeputyKey=${DeputyKey}`,
		"GET",
	)
}

export const ReadIntroduceContent = function(SubKey) { // 了解星梦圆详情函数
	// return new Promise((resolve, reject) => {
	// 	uni.request({
	// 		url: `${baseUrl}ReadIntroduceContent?SubKey=${SubKey}`,
	// 		method:"GET",
	// 		success: (res) => {
	// 			resolve(res.data);
	// 		},
	// 		fail: (res) => {
	// 			reject(res);
	// 		}
	// 	})
	// })
	return MyRequest(
		`${baseUrl}ReadIntroduceContent?SubKey=${SubKey}`,
		"GET",
	)
}
export const EstablishWearerInfor = function(form) { // 添加佩戴者信息函数
	return MyRequest(
		`${baseUrl}EstablishWearerInfor`,
		"POST",
		form,
	)
}

export const ReadWearerInfor = function(MiniOpenId) { // 读取佩戴者信息函数
	return MyRequest(
		`${baseUrl}ReadWearerInfor?MiniOpenId=${MiniOpenId}`,
		"GET",
	)
}
export const EditVisionArchivesInfor = function(form) { // 添加/修改视力档案信息函数
	return MyRequest(
		`${baseUrl}EditVisionArchivesInfor`,
		"POST",
		form,
	)
}
/**
 * 
 * @param {*} SmallOpenId 会员小程序OPENID
 * @param {*} WearerNo  佩戴者编号
 * @param {*} VhNo 视力档案编号
 * @returns 
 */
export const ReMoveVisionArchivesInfor = function(SmallOpenId, WearerNo, VhNo) { // 删除视力档案信息函数
	return MyRequest(
		`${baseUrl}ReMoveVisionArchivesInfor?SmallOpenId=${SmallOpenId}&WearerNo=${WearerNo}&VhNo=${VhNo}`,
		"POST",
	)
}
export const ReadVisionArchivesInfor = function(SmallOpenId, WearerId) { // 读取视力档案信息函数
	return MyRequest(
		`${baseUrl}ReadVisionArchivesInfor?SmallOpenId=${SmallOpenId}&WearerId=${WearerId}`,
		"GET",
	)
}
export const SecurityCodeCheck = function(Barcode) { // 防伪码查询函数
	return MyRequest(
		`${baseUrl}SecurityCodeCheck?Barcode=${Barcode}`,
		"GET",
	)
}
export const StarDreambActivate = function(form) { // 星梦保激活视力档案信息函数
	return MyRequest(
		`${baseUrl}StarDreambActivate`,
		"POST",
		form,
	)
}
/**
 * 
 * @param {*} SmallOpenId 
 * @param {*} WearerCode 佩戴者编号
 * @returns 
 */
export const ReadStarDreambInfor = function(SmallOpenId, WearerCode) { // 读取星梦保视力档案信息函数
	return MyRequest(
		`${baseUrl}ReadStarDreambHisInfor?SmallOpenId=${SmallOpenId}&WearerCode=${WearerCode}`,
		"GET",
	)
}
/**
 * 
 * @param {*} MiniOpenId 
 * @param {*} OpType //操作类型(固定值：添加/移除)
 * @param {*} CollectType //收藏类型(固定值：门店/文章)
 * @param {*} TypeCode //内容编号(收藏类型为门店，则为：门店编号；收藏类型为文章，则为：详情内容说明编号
 * @returns 
 */
export const EditMyCollect = function(MiniOpenId, OpType, CollectType, TypeCode) { // 添加/移除收藏内容函数
	return MyRequest(
		`${baseUrl}EditMyCollect`,
		"POST",
		{
			MiniOpenId,
			OpType,
			CollectType,
			TypeCode,
		}
	)
}
/**
 * 
 * @param {*} SmallOpenId 
 * @param {*} CollectType 收藏类型(门店/文章)
 */
export const ReadMyCollect = function(SmallOpenId, CollectType) { // 读取收藏内容函数
	return MyRequest(
		`${baseUrl}ReadMyCollect?SmallOpenId=${SmallOpenId}&CollectType=${CollectType}`,
		"GET",
	)
}
export const ReadChildInforProtectRule = function() { // 星梦圆儿童个人信息保护规则及监护人须知函数
	return MyRequest(
		`${baseUrl}ReadChildInforProtectRule`,
		"GET",
	)
}
export const ReadTermAgreement = function() { // 条款协议函数
	return MyRequest(
		`${baseUrl}ReadTermAgreement`,
		"GET",
	)
}
export const StarDreambVisionReview = function(form) { // 星梦保激活视力复查添加/编辑函数
	return MyRequest(
		`${baseUrl}StarDreambVisionReview`,
		"POST",
		form,
	)
}
export const ReadStarDreambCurInfor = function(SmallOpenId, WearerCode) { // 读取星梦保激活当前待换新片档案信息函数
	return MyRequest(
		`${baseUrl}ReadStarDreambCurInfor?SmallOpenId=${SmallOpenId}&WearerCode=${WearerCode}`,
		"GET",
	)
}
export const ReadStarDreambVisionReviewInfor = function(SmallOpenId, WearerCode, ActivateVhId) { // 读取星梦保激活当前待换新片档案视力复查信息函数
	return MyRequest(
		`${baseUrl}ReadStarDreambVisionReviewInfor?SmallOpenId=${SmallOpenId}&WearerCode=${WearerCode}&ActivateVhId=${ActivateVhId}`,
		"GET",
	)
}
export const ReadMemberReceive = function(SmallOpenId) { // 读取收货地址信息函数
	return MyRequest(
		`${baseUrl}ReadMemberReceive?SmallOpenId=${SmallOpenId}`,
		"GET",
	)
}
export const ApplyChangeLensSubmit = function(form) { // 星梦保激活视力复查换新片申请函数
	return MyRequest(
		`${baseUrl}ApplyChangeLensSubmit`,
		"POST",
		form,
	)
}